{extend name="common/base" /}
{block name="body"}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">角色列表</div>
                        <div class="layui-card-body admin-table">
                            <div class="layui-row admin-table-action">
                                <div class="layui-col-md6 tool-action">
                                    <a data-href="{:url('admin/group/add')}"
                                       data-title="新增角色"
                                       admin-event="formLayer"
                                       class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary">
                                        <i class="layui-icon layui-icon-add-circle"></i>
                                    </a>
                                    <button id="layui-icon-refresh"
                                            class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary">
                                        <i class="layui-icon layui-icon-refresh"></i>
                                    </button>
                                </div>
                                <div class="layui-col-md6 search-form">
                                    <div class="layui-form layui-form-pane search">
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">搜索</label>
                                            <div class="layui-input-inline">
                                                <input type="text" name="keywords" lay-verify="required"
                                                       placeholder="请输入" autocomplete="off" class="layui-input">
                                            </div>
                                            <button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary search-submit">
                                                <i class="layui-icon layui-icon-search"></i>
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div id="list-table" lay-filter="list-table"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="my_js"}
<script type="text/html" id="menu">
    <a class="layui-btn layui-btn-xs layui-bg-gray" admin-event="formLayer" data-href="/admin/group/edit/{{d.id}}.html">编辑</a>
    <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="del">删除</a>
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="ON|OFF" lay-filter="status" {{
           d.status== 1 ? 'checked' : '' }} {{
    d.id == 1 ? 'disabled' : '' }}>
</script>
<script>
    layui.use(['element', 'admin', 'table', 'form'], function () {
        var table = layui.table
            , admin = layui.admin
            , form = layui.form
            , $ = layui.$;
        var list_table = admin.table(table, 'list-table', 'admin/group', {
            page: false
            , cols: [[
                {field: 'id', title: 'ID', width: 80}
                , {field: 'title', title: '角色名称', minWidth: 200}
                , {field: 'description', title: '描述', minWidth: 200}
                , {field: 'status', title: '状态', templet: '#status', width: 100}
                , {fixed: 'right', align: 'center', templet: '#menu', width: 120}
            ]]
        });
        form.on('switch(status)', function (obj) {
            admin.ajax('admin/group/toggle/' + this.value, '', function (data) {
                if (data.code == 1001) {
                    layer.msg(data.msg);
                } else {
                    layer.msg(data.msg);
                }
            }, '', 'get');
        });
        $('#layui-icon-refresh').click(function () {
            list_table.reload();
        });
        admin.enter(function () {
            $('.search-submit').click();
        });
        $('.search-submit').click(function () {
            var keyword = $("input[name='keywords']").val();
            list_table.reload({
                where: {
                    keyword: keyword
                }
            });
        });
        table.on('tool(list-table)', function (obj) {
            var data = obj.data;            //获得当前行数据
            var layEvent = obj.event;       //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent === 'del'){
                layer.confirm('确定删除该权限组？', {
                    btn: ['确定','取消'] //按钮
                }, function(index){
                    admin.ajax('admin/group/'+data.id,'',function (data) {
                        if (data.code == 1001) {
                            obj.del();
                            layer.close(index);
                        } else {
                            layer.msg(data.msg);
                        }
                    },'','DELETE');
                });
            }
        });

    });
</script>
{/block}